/*
 * Copyright DimYLiK.
 */

package com.example.myapp.shared;

import com.example.myapp.client.AntiBaldaView;
import com.google.gwt.user.client.rpc.IsSerializable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Word implements Serializable, IsSerializable, Comparable {

    private static final long serialVersionUID = 7526472295622776147L;

    public static final String MOVE_BEGIN = "[";
    public static final String MOVE_DELIMITER = ":";
    public static final String MOVE_END = "]";

    public static final Word WORD_X = new Word(AntiBaldaView.X);

    private String word;
    private String move;

    public Word() {
    }

    public Word(String word, String move) {
        this.word = word.toLowerCase();
        this.move = move;
    }

    public Word(String word) {
        this.word = word.toLowerCase();
    }

    public String getMove() {
        return move;
    }

    public void setMove(String move) {
        this.move = move;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Word)) return false;

        Word that = (Word) o;
        return word.equals(that.word);
    }

    @Override
    public int hashCode() {
        return word.hashCode();
    }

    @Override
    public String toString() {
        return word;// + " " + move;
    }

    public int compareTo(Object word) {
        return Integer.valueOf(((Word) word).getWord().length()).compareTo(this.getWord().length());
    }

    public static String logCurrentMove(int i, int j) {
        return MOVE_BEGIN + String.valueOf(i + 1) + MOVE_DELIMITER + String.valueOf(j + 1) + MOVE_END;
    }

    public static List<List<Integer>> parseMoves(String wordMove) {
        List<List<Integer>> movesList = new ArrayList<List<Integer>>();
        String[] moves = wordMove.split("-");
        for (String move : moves) {
            int moveBegin = move.indexOf(MOVE_BEGIN);
            int moveDelimiter = move.indexOf(MOVE_DELIMITER);
            int moveEnd = move.indexOf(MOVE_END);

            String i = move.substring(moveBegin + 1, moveDelimiter);
            String j = move.substring(moveDelimiter + 1, moveEnd);
            movesList.add(Arrays.asList((Integer.valueOf(i) - 1), (Integer.valueOf(j) - 1)));
        }
        return movesList;
    }
}
